from xml.dom.minidom import parse
import gym
import re

class Dotsdict(dict):
    def __init__(self, args, **kwargs):
        super(Dotsdict, self).__init__(args, **kwargs)
        for obj in [args, kwargs]:
            for k, v in obj.items():
                if isinstance(v, dict):
                    v = Dotsdict(v)
                self.__setitem__(k, v)

    def __setitem__(self, key, val):
        super(Dotsdict, self).__setitem__(key, val)
        # self.__dict__[key] = val

    def __delitem__(self, key):
        super(Dotsdict, self).__delitem__(key)
        # del self.__dict__[key]

    def __getitem__(self, key):
        return super(Dotsdict, self).__getitem__(key)

    def __missing__(self, key):
        dots = Dotsdict()
        self.__setitem__(key, dots)
        return dots

    __setattr__, __delattr__, __getattr__ = __setitem__, __delitem__, __getitem__



def controller_init_(self, xml_file):
    # 获取 xml 文档对象
    domTree = parse(xml_file)
    # 获得根节点
    rootNode = domTree.documentElement

    self.env = gym.make(rootNode.getElementsByTagName('env')[0].firstChild.data)

    self.conf = {'env': rootNode.getElementsByTagName('env')[0].firstChild.data}

    for node in rootNode.getElementsByTagName('int')[0].childNodes:
        if node.nodeType !=1:
            continue
        self.conf[node.nodeName] = int(node.firstChild.data)
    for node in rootNode.getElementsByTagName('float')[0].childNodes:
        if node.nodeType !=1:
            continue
        self.conf[node.nodeName] = float(node.firstChild.data)

    self.conf = Dotsdict(self.conf)

    if self.conf:
        return True
    else:
        return False
# class aa():
#     def __init__(self):
#         self.env = ''
#         self.conf = {}
#         controller_init_(self,"../conf/env_conf.xml")
#
# if __name__ == '__main__':
#     aaa = aa()